package cn.com.easyfelling.utils;

import java.sql.*;

public class DBUtils {

  public static Connection getConnection() throws Exception {
    // 1.配置4个基本要素
    String url = "jdbc:mysql://120.79.55.11:3306/easyfeeling?serverTimezone=Asia/Shanghai";
    String user = "easyfeeling";
    String password = "iJ5CwzCGPmwPwMPN";
    String driverName = "com.mysql.cj.jdbc.Driver";
    // 通过反射实例化Driver，可省略
    Driver driver = (Driver)Class.forName(driverName).getConstructor().newInstance();
    // 注册驱动，可省略
    DriverManager.registerDriver(driver);

    return DriverManager.getConnection(url,user,password);
  }


  /**
   * 关闭资源
   * @param connection
   * @param statement
   * @param resultSet
   */
  public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){

      try {
        if (resultSet != null) {
          resultSet.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }

      try {
        if (statement != null) {
          statement.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    try {
      if (connection != null) {
        connection.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

  }
}
